﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ActionViewer.Models;
using System.Collections;

namespace ActionViewer.Controllers
{
    public class HomeController : Controller
    {
        private ArrayList list;
        private const int pageSize = 2;
        public HomeController()
        {
            list=new ArrayList();
        }
        public ViewResult Index(int? page)
        {                        
            MountainInfo m1=new MountainInfo();
            m1.name="Everest";
            m1.height=8848;
            list.Add(m1);
            m1 = new MountainInfo();
            m1.name = "Elbrus";
            m1.height = 5642;
            list.Add(m1);
            m1 = new MountainInfo();
            m1.name = "Kalimanjaro";
            m1.height = 5895;
            list.Add(m1);
            m1 = new MountainInfo();
            m1.name = "Aconcague";
            m1.height = 6962;
            list.Add(m1);
            m1 = new MountainInfo();
            m1.name = "Mount Kinley";
            m1.height = 6194;
            list.Add(m1);
            
            ViewBag.currentPage = page ?? 1;
            ViewBag.totalPages = (int)Math.Ceiling(1.0 * list.Count / pageSize);
            if (page >= 1)
            {
                if (((page ?? 1) - 1) * pageSize + pageSize > list.Count)
                {
                    int rest = list.Count - ((page ?? 1) - 1) * pageSize;
                    if (rest > 0)
                    {
                        ViewBag.mountains = list.GetRange(((page ?? 1) - 1) * pageSize, rest);
                    }
                    else
                    {
                        ViewBag.mountains = new ArrayList();
                    }
                }
                else
                {
                    ViewBag.mountains = list.GetRange(((page ?? 1) - 1) * pageSize, pageSize);
                }
            }
            else
            {
                ViewBag.mountains = new ArrayList();
            }
            return View();
        }        
        public ViewResult DeleteItem(String name)
        {            
            ViewBag.name = name;
            return View();
        }
    }
}
